package com.aluvfy.jdbc.dao;

import com.aluvfy.jdbc.beans.Employee;

import java.util.List;

/**
 * @author: aluvfy
 * @description: 完成数据表t_employee的增删改查
 * 增删改查 也被称为 CRUD
 *
 * 注意：
 *   DAO不负责任何业务逻辑的处理，只负责CRUD操作
 *   DAO是JavaEE的设计模式之一
 *   DAO中起名一般以insert delete update select 开头
 * @date: 2024/10/21 17:52
 */
public class EmployeeDao extends BaseDao{
    /**
     * 插入员工
     * @param employee 员工数据
     * @return 1表示新增成功，返回其他值表示失败
     */
    /*public int insert(Employee employee) {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = DbUtils.getConnection();
            String sql = "insert into t_employee(name, job, salary, hiredate, address) values(?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, employee.getName());
            ps.setString(2, employee.getJob());
            ps.setDouble(3, employee.getSalary());
            ps.setString(4, employee.getHiredate());
            ps.setString(5, employee.getAddress());
            count = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.close(conn, ps, null);
        }
        return count;
    }*/

    public int insert(Employee employee) {
        String sql = "insert into t_employee(name, job, salary, hiredate, address) values(?,?,?,?,?)";
        return executeUpdate(sql,employee.getName(),employee.getJob(),employee.getSalary(),employee.getHiredate(),employee.getAddress());
    }

    /**
     * 删除员工
     * @param id 员工id
     * @return 1表示删除成功，返回其他值表示失败
     */
    /*public int deleteById(Long id) {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = DbUtils.getConnection();
            String sql = "delete from t_employee where id=?";
            ps = conn.prepareStatement(sql);
            ps.setLong(1, id);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.close(conn, ps, null);
        }
        return count;
    }*/
    public int deleteById(Long id) {
        String sql = "delete from t_employee where id=?";
        return executeUpdate(sql,id);
    }

    /**
     * 修改员工
     * @param newemployee 新增员工的信息
     * @return 1表示修改成功，返回其他值表示失败
     */
    /*public int update(Employee newemployee) {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = DbUtils.getConnection();
            String sql = "update t_employee set name=?, job=?, salary=?, hiredate=?, address=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, newemployee.getName());
            ps.setString(2, newemployee.getJob());
            ps.setDouble(3, newemployee.getSalary());
            ps.setString(4, newemployee.getHiredate());
            ps.setString(5, newemployee.getAddress());
            ps.setLong(6, newemployee.getId());
            count = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.close(conn, ps, null);
        }
        return count;
    }*/
    public int update(Employee newemployee) {
        String sql = "update t_employee set name=?, job=?, salary=?, hiredate=?, address=? where id=?";
        return  executeUpdate(sql,newemployee.getName(),newemployee.getJob(),newemployee.getSalary(),newemployee.getHiredate(),newemployee.getAddress(),newemployee.getId());
    }

    /**
     * 根据员工id获取员工信息
     * @param id 员工id
     * @return 员工信息
     */
    public Employee selectById(Long id) {
        /*Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Employee employee = null;

        try {
            conn = DbUtils.getConnection();
            String sql = "select * from t_employee where id=?";
            ps = conn.prepareStatement(sql);
            ps.setLong(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                //完成数据的封装，将数据库表中查出来数据封装为成员对象
                employee = new Employee();
                employee.setId(rs.getLong("id"));
                employee.setName(rs.getString("name"));
                employee.setJob(rs.getString("job"));
                employee.setSalary(rs.getDouble("salary"));
                employee.setHiredate(rs.getString("hiredate"));
                employee.setAddress(rs.getString("address"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.close(conn, ps, rs);
        }
        return employee;*/
        String sql = "select * from t_employee where id=?";
        return queryOne(Employee.class,sql,id);
    }

    /**
     * 获取所有员工信息
     * @return 员工列表
     */
    public List<Employee> selectAll() {

        /*Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Employee> employeeList = new ArrayList<>();

        try {
            conn = DbUtils.getConnection();
            String sql = "select * from t_employee";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                //完成数据的封装，将数据库表中查出来数据封装为成员对象
                Employee employee = new Employee();
                employee.setId(rs.getLong("id"));
                employee.setName(rs.getString("name"));
                employee.setJob(rs.getString("job"));
                employee.setSalary(rs.getDouble("salary"));
                employee.setHiredate(rs.getString("hiredate"));
                employee.setAddress(rs.getString("address"));
                //将封装好的对象添加到集合中
                employeeList.add(employee);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.close(conn, ps, rs);
        }
        return employeeList;*/
        String sql = "select id, name, job, salary, hiredate, address from t_employee";
        return executeQuery(Employee.class,sql);
    }
}
